package com.squapsource.project_euler;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Problem24 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		List<String> strings = new ArrayList<String>();
		List<String> digits = Arrays.asList("0", "1", "2", "3", "4","5","6","7","8","9");
		strings = getNumbers(digits);
		Collections.sort(strings);
		System.out.println(strings.get(999999));

	}

	private static List<String> getNumbers(List<String> digits) {
		List<String> strings = new ArrayList<String>();
		if (digits.size() == 1)
			strings.addAll(digits);
		else {
			for (String digit : digits) {
				for (String things : getNumbers(getNewSet(digits, digit))) {
					strings.add(digit + things);
				}
			}
		}
		return strings;

	}

	private static List<String> getNewSet(List<String> digits, String used) {
		List<String> newDigits = new ArrayList<String>();
		newDigits.addAll(digits);
		newDigits.remove(used);
		return newDigits;
	}

}
